using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using SheLife.Domain.Interfaces;
using SheLife.Domain.Interfaces.Repositories;

namespace SheLife.Application.Services
{
    /// <summary>
    /// 成长目标服务实现
    /// </summary>
    public class GrowthGoalService : IGrowthGoalService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthGoalService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthGoal?> GetGrowthGoalByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthGoalRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthGoal>> GetGrowthGoalsByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthGoalRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthGoal>> GetGrowthGoalsByUserIdAndStatusAsync(Guid userId, string status)
        {
            return await _unitOfWork.GrowthGoalRepository.GetByUserIdAndStatusAsync(userId, status);
        }

        public async Task<IEnumerable<GrowthGoal>> GetGrowthGoalsByUserIdAndCategoryAsync(Guid userId, string category)
        {
            return await _unitOfWork.GrowthGoalRepository.GetByUserIdAndCategoryAsync(userId, category);
        }

        public async Task<GrowthGoal> CreateGrowthGoalAsync(Guid userId, string title, string description, string category, DateTime? targetDate = null, string priority = "中等")
        {
            var growthGoal = new GrowthGoal
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                Title = title,
                Description = description,
                Category = category,
                TargetDate = targetDate,
                Priority = priority,
                Status = "进行中",
                Progress = 0,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthGoalRepository.AddAsync(growthGoal);
            await _unitOfWork.SaveChangesAsync();

            return growthGoal;
        }

        public async Task<GrowthGoal?> UpdateGrowthGoalAsync(Guid id, string? title, string? description, string? category, DateTime? targetDate, string? priority, string? status, int? progress)
        {
            var growthGoal = await _unitOfWork.GrowthGoalRepository.GetByIdAsync(id);
            if (growthGoal == null)
                return null;

            if (title != null) growthGoal.Title = title;
            if (description != null) growthGoal.Description = description;
            if (category != null) growthGoal.Category = category;
            if (targetDate.HasValue) growthGoal.TargetDate = targetDate.Value;
            if (priority != null) growthGoal.Priority = priority;
            if (status != null) growthGoal.Status = status;
            if (progress.HasValue) growthGoal.Progress = progress.Value;

            growthGoal.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthGoalRepository.UpdateAsync(growthGoal);
            await _unitOfWork.SaveChangesAsync();

            return growthGoal;
        }

        public async Task<bool> DeleteGrowthGoalAsync(Guid id)
        {
            var growthGoal = await _unitOfWork.GrowthGoalRepository.GetByIdAsync(id);
            if (growthGoal == null)
                return false;

            await _unitOfWork.GrowthGoalRepository.DeleteAsync(growthGoal);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }

        public async Task<GrowthGoal?> UpdateGrowthGoalProgressAsync(Guid id, int progress)
        {
            var growthGoal = await _unitOfWork.GrowthGoalRepository.GetByIdAsync(id);
            if (growthGoal == null)
                return null;

            growthGoal.Progress = progress;
            if (progress >= 100)
                growthGoal.Status = "已完成";

            growthGoal.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthGoalRepository.UpdateAsync(growthGoal);
            await _unitOfWork.SaveChangesAsync();

            return growthGoal;
        }
    }

    /// <summary>
    /// 成长目标任务服务实现
    /// </summary>
    public class GrowthGoalTaskService : IGrowthGoalTaskService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthGoalTaskService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthGoalTask?> GetGrowthGoalTaskByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthGoalTaskRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthGoalTask>> GetGrowthGoalTasksByGoalIdAsync(Guid goalId)
        {
            return await _unitOfWork.GrowthGoalTaskRepository.GetByGoalIdAsync(goalId);
        }

        public async Task<IEnumerable<GrowthGoalTask>> GetGrowthGoalTasksByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthGoalTaskRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthGoalTask>> GetGrowthGoalTasksByUserIdAndStatusAsync(Guid userId, string status)
        {
            return await _unitOfWork.GrowthGoalTaskRepository.GetByUserIdAndStatusAsync(userId, status);
        }

        public async Task<GrowthGoalTask> CreateGrowthGoalTaskAsync(Guid goalId, string title, string description, DateTime? dueDate = null, string priority = "中等")
        {
            var growthGoalTask = new GrowthGoalTask
            {
                Id = Guid.NewGuid(),
                GoalId = goalId,
                Title = title,
                Description = description,
                DueDate = dueDate,
                Priority = priority,
                Status = "待处理",
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthGoalTaskRepository.AddAsync(growthGoalTask);
            await _unitOfWork.SaveChangesAsync();

            return growthGoalTask;
        }

        public async Task<GrowthGoalTask?> UpdateGrowthGoalTaskAsync(Guid id, string? title, string? description, DateTime? dueDate, string? priority, string? status)
        {
            var growthGoalTask = await _unitOfWork.GrowthGoalTaskRepository.GetByIdAsync(id);
            if (growthGoalTask == null)
                return null;

            if (title != null) growthGoalTask.Title = title;
            if (description != null) growthGoalTask.Description = description;
            if (dueDate.HasValue) growthGoalTask.DueDate = dueDate.Value;
            if (priority != null) growthGoalTask.Priority = priority;
            if (status != null) growthGoalTask.Status = status;

            growthGoalTask.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthGoalTaskRepository.UpdateAsync(growthGoalTask);
            await _unitOfWork.SaveChangesAsync();

            return growthGoalTask;
        }

        public async Task<bool> DeleteGrowthGoalTaskAsync(Guid id)
        {
            var growthGoalTask = await _unitOfWork.GrowthGoalTaskRepository.GetByIdAsync(id);
            if (growthGoalTask == null)
                return false;

            await _unitOfWork.GrowthGoalTaskRepository.DeleteAsync(growthGoalTask);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }

        public async Task<GrowthGoalTask?> CompleteGrowthGoalTaskAsync(Guid id)
        {
            var growthGoalTask = await _unitOfWork.GrowthGoalTaskRepository.GetByIdAsync(id);
            if (growthGoalTask == null)
                return null;

            growthGoalTask.Status = "已完成";
            growthGoalTask.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthGoalTaskRepository.UpdateAsync(growthGoalTask);
            await _unitOfWork.SaveChangesAsync();

            return growthGoalTask;
        }
    }

    /// <summary>
    /// 成长记录服务实现
    /// </summary>
    public class GrowthRecordService : IGrowthRecordService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthRecordService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthRecord?> GetGrowthRecordByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthRecordRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthRecord>> GetGrowthRecordsByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthRecordRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthRecord>> GetGrowthRecordsByUserIdAndDateRangeAsync(Guid userId, DateTime startDate, DateTime endDate)
        {
            return await _unitOfWork.GrowthRecordRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);
        }

        public async Task<IEnumerable<GrowthRecord>> GetGrowthRecordsByUserIdAndCategoryAsync(Guid userId, string category)
        {
            return await _unitOfWork.GrowthRecordRepository.GetByUserIdAndCategoryAsync(userId, category);
        }

        public async Task<GrowthRecord> CreateGrowthRecordAsync(Guid userId, string title, string content, string category, string mood, string? imageUrl = null)
        {
            var growthRecord = new GrowthRecord
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                Title = title,
                Content = content,
                Category = category,
                Mood = mood,
                ImageUrl = imageUrl,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthRecordRepository.AddAsync(growthRecord);
            await _unitOfWork.SaveChangesAsync();

            return growthRecord;
        }

        public async Task<GrowthRecord?> UpdateGrowthRecordAsync(Guid id, string? title, string? content, string? category, string? mood, string? imageUrl = null)
        {
            var growthRecord = await _unitOfWork.GrowthRecordRepository.GetByIdAsync(id);
            if (growthRecord == null)
                return null;

            if (title != null) growthRecord.Title = title;
            if (content != null) growthRecord.Content = content;
            if (category != null) growthRecord.Category = category;
            if (mood != null) growthRecord.Mood = mood;
            if (imageUrl != null) growthRecord.ImageUrl = imageUrl;

            growthRecord.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthRecordRepository.UpdateAsync(growthRecord);
            await _unitOfWork.SaveChangesAsync();

            return growthRecord;
        }

        public async Task<bool> DeleteGrowthRecordAsync(Guid id)
        {
            var growthRecord = await _unitOfWork.GrowthRecordRepository.GetByIdAsync(id);
            if (growthRecord == null)
                return false;

            await _unitOfWork.GrowthRecordRepository.DeleteAsync(growthRecord);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }
    }

    /// <summary>
    /// 成长里程碑服务实现
    /// </summary>
    public class GrowthMilestoneService : IGrowthMilestoneService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthMilestoneService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthMilestone?> GetGrowthMilestoneByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthMilestoneRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthMilestone>> GetGrowthMilestonesByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthMilestoneRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthMilestone>> GetGrowthMilestonesByUserIdAndCategoryAsync(Guid userId, string category)
        {
            return await _unitOfWork.GrowthMilestoneRepository.GetByUserIdAndCategoryAsync(userId, category);
        }

        public async Task<GrowthMilestone> CreateGrowthMilestoneAsync(Guid userId, string title, string description, string category, DateTime achievedDate, string? imageUrl = null)
        {
            var growthMilestone = new GrowthMilestone
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                Title = title,
                Description = description,
                Category = category,
                AchievedDate = achievedDate,
                ImageUrl = imageUrl,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthMilestoneRepository.AddAsync(growthMilestone);
            await _unitOfWork.SaveChangesAsync();

            return growthMilestone;
        }

        public async Task<GrowthMilestone?> UpdateGrowthMilestoneAsync(Guid id, string? title, string? description, string? category, DateTime? achievedDate, string? imageUrl = null)
        {
            var growthMilestone = await _unitOfWork.GrowthMilestoneRepository.GetByIdAsync(id);
            if (growthMilestone == null)
                return null;

            if (title != null) growthMilestone.Title = title;
            if (description != null) growthMilestone.Description = description;
            if (category != null) growthMilestone.Category = category;
            if (achievedDate.HasValue) growthMilestone.AchievedDate = achievedDate.Value;
            if (imageUrl != null) growthMilestone.ImageUrl = imageUrl;

            growthMilestone.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthMilestoneRepository.UpdateAsync(growthMilestone);
            await _unitOfWork.SaveChangesAsync();

            return growthMilestone;
        }

        public async Task<bool> DeleteGrowthMilestoneAsync(Guid id)
        {
            var growthMilestone = await _unitOfWork.GrowthMilestoneRepository.GetByIdAsync(id);
            if (growthMilestone == null)
                return false;

            await _unitOfWork.GrowthMilestoneRepository.DeleteAsync(growthMilestone);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }
    }

    /// <summary>
    /// 成长计划服务实现
    /// </summary>
    public class GrowthPlanService : IGrowthPlanService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthPlanService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthPlan?> GetGrowthPlanByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthPlanRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthPlan>> GetGrowthPlansByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthPlanRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthPlan>> GetGrowthPlansByUserIdAndStatusAsync(Guid userId, string status)
        {
            return await _unitOfWork.GrowthPlanRepository.GetByUserIdAndStatusAsync(userId, status);
        }

        public async Task<IEnumerable<GrowthPlan>> GetGrowthPlansByUserIdAndCategoryAsync(Guid userId, string category)
        {
            return await _unitOfWork.GrowthPlanRepository.GetByUserIdAndCategoryAsync(userId, category);
        }

        public async Task<GrowthPlan> CreateGrowthPlanAsync(Guid userId, string title, string description, string category, DateTime startDate, DateTime endDate)
        {
            var growthPlan = new GrowthPlan
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                Title = title,
                Description = description,
                Category = category,
                StartDate = startDate,
                EndDate = endDate,
                Status = "进行中",
                Progress = 0,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthPlanRepository.AddAsync(growthPlan);
            await _unitOfWork.SaveChangesAsync();

            return growthPlan;
        }

        public async Task<GrowthPlan?> UpdateGrowthPlanAsync(Guid id, string? title, string? description, string? category, DateTime? startDate, DateTime? endDate, string? status, int? progress)
        {
            var growthPlan = await _unitOfWork.GrowthPlanRepository.GetByIdAsync(id);
            if (growthPlan == null)
                return null;

            if (title != null) growthPlan.Title = title;
            if (description != null) growthPlan.Description = description;
            if (category != null) growthPlan.Category = category;
            if (startDate.HasValue) growthPlan.StartDate = startDate.Value;
            if (endDate.HasValue) growthPlan.EndDate = endDate.Value;
            if (status != null) growthPlan.Status = status;
            if (progress.HasValue) growthPlan.Progress = progress.Value;

            growthPlan.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthPlanRepository.UpdateAsync(growthPlan);
            await _unitOfWork.SaveChangesAsync();

            return growthPlan;
        }

        public async Task<bool> DeleteGrowthPlanAsync(Guid id)
        {
            var growthPlan = await _unitOfWork.GrowthPlanRepository.GetByIdAsync(id);
            if (growthPlan == null)
                return false;

            await _unitOfWork.GrowthPlanRepository.DeleteAsync(growthPlan);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }

        public async Task<GrowthPlan?> UpdateGrowthPlanProgressAsync(Guid id, int progress)
        {
            var growthPlan = await _unitOfWork.GrowthPlanRepository.GetByIdAsync(id);
            if (growthPlan == null)
                return null;

            growthPlan.Progress = progress;
            if (progress >= 100)
                growthPlan.Status = "已完成";

            growthPlan.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthPlanRepository.UpdateAsync(growthPlan);
            await _unitOfWork.SaveChangesAsync();

            return growthPlan;
        }
    }

    /// <summary>
    /// 成长计划步骤服务实现
    /// </summary>
    public class GrowthPlanStepService : IGrowthPlanStepService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthPlanStepService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthPlanStep?> GetGrowthPlanStepByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthPlanStepRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthPlanStep>> GetGrowthPlanStepsByPlanIdAsync(Guid planId)
        {
            return await _unitOfWork.GrowthPlanStepRepository.GetByPlanIdAsync(planId);
        }

        public async Task<IEnumerable<GrowthPlanStep>> GetGrowthPlanStepsByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthPlanStepRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthPlanStep>> GetGrowthPlanStepsByUserIdAndStatusAsync(Guid userId, string status)
        {
            return await _unitOfWork.GrowthPlanStepRepository.GetByUserIdAndStatusAsync(userId, status);
        }

        public async Task<GrowthPlanStep> CreateGrowthPlanStepAsync(Guid planId, string title, string description, int order, DateTime? dueDate = null)
        {
            var growthPlanStep = new GrowthPlanStep
            {
                Id = Guid.NewGuid(),
                PlanId = planId,
                Title = title,
                Description = description,
                Order = order,
                DueDate = dueDate,
                Status = "待处理",
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthPlanStepRepository.AddAsync(growthPlanStep);
            await _unitOfWork.SaveChangesAsync();

            return growthPlanStep;
        }

        public async Task<GrowthPlanStep?> UpdateGrowthPlanStepAsync(Guid id, string? title, string? description, int? order, DateTime? dueDate, string? status)
        {
            var growthPlanStep = await _unitOfWork.GrowthPlanStepRepository.GetByIdAsync(id);
            if (growthPlanStep == null)
                return null;

            if (title != null) growthPlanStep.Title = title;
            if (description != null) growthPlanStep.Description = description;
            if (order.HasValue) growthPlanStep.Order = order.Value;
            if (dueDate.HasValue) growthPlanStep.DueDate = dueDate.Value;
            if (status != null) growthPlanStep.Status = status;

            growthPlanStep.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthPlanStepRepository.UpdateAsync(growthPlanStep);
            await _unitOfWork.SaveChangesAsync();

            return growthPlanStep;
        }

        public async Task<bool> DeleteGrowthPlanStepAsync(Guid id)
        {
            var growthPlanStep = await _growthPlanStepRepository.GetByIdAsync(id);
            if (growthPlanStep == null)
                return false;

            await _growthPlanStepRepository.DeleteAsync(growthPlanStep);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }

        public async Task<GrowthPlanStep?> CompleteGrowthPlanStepAsync(Guid id)
        {
            var growthPlanStep = await _growthPlanStepRepository.GetByIdAsync(id);
            if (growthPlanStep == null)
                return null;

            growthPlanStep.Status = "已完成";
            growthPlanStep.UpdatedAt = DateTime.UtcNow;

            await _growthPlanStepRepository.UpdateAsync(growthPlanStep);
            await _unitOfWork.SaveChangesAsync();

            return growthPlanStep;
        }
    }

    /// <summary>
    /// 成长习惯服务实现
    /// </summary>
    public class GrowthHabitService : IGrowthHabitService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthHabitService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthHabit?> GetGrowthHabitByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthHabitRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthHabit>> GetGrowthHabitsByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthHabitRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthHabit>> GetGrowthHabitsByUserIdAndCategoryAsync(Guid userId, string category)
        {
            return await _unitOfWork.GrowthHabitRepository.GetByUserIdAndCategoryAsync(userId, category);
        }

        public async Task<GrowthHabit> CreateGrowthHabitAsync(Guid userId, string title, string description, string category, string frequency, int targetValue)
        {
            var growthHabit = new GrowthHabit
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                Title = title,
                Description = description,
                Category = category,
                Frequency = frequency,
                TargetValue = targetValue,
                IsActive = true,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthHabitRepository.AddAsync(growthHabit);
            await _unitOfWork.SaveChangesAsync();

            return growthHabit;
        }

        public async Task<GrowthHabit?> UpdateGrowthHabitAsync(Guid id, string? title, string? description, string? category, string? frequency, int? targetValue, bool? isActive)
        {
            var growthHabit = await _unitOfWork.GrowthHabitRepository.GetByIdAsync(id);
            if (growthHabit == null)
                return null;

            if (title != null) growthHabit.Title = title;
            if (description != null) growthHabit.Description = description;
            if (category != null) growthHabit.Category = category;
            if (frequency != null) growthHabit.Frequency = frequency;
            if (targetValue.HasValue) growthHabit.TargetValue = targetValue.Value;
            if (isActive.HasValue) growthHabit.IsActive = isActive.Value;

            growthHabit.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthHabitRepository.UpdateAsync(growthHabit);
            await _unitOfWork.SaveChangesAsync();

            return growthHabit;
        }

        public async Task<bool> DeleteGrowthHabitAsync(Guid id)
        {
            var growthHabit = await _unitOfWork.GrowthHabitRepository.GetByIdAsync(id);
            if (growthHabit == null)
                return false;

            await _unitOfWork.GrowthHabitRepository.DeleteAsync(growthHabit);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }
    }

    /// <summary>
    /// 成长习惯记录服务实现
    /// </summary>
    public class GrowthHabitRecordService : IGrowthHabitRecordService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthHabitRecordService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthHabitRecord?> GetGrowthHabitRecordByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthHabitRecordRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthHabitRecord>> GetGrowthHabitRecordsByHabitIdAsync(Guid habitId)
        {
            return await _unitOfWork.GrowthHabitRecordRepository.GetByHabitIdAsync(habitId);
        }

        public async Task<IEnumerable<GrowthHabitRecord>> GetGrowthHabitRecordsByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthHabitRecordRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthHabitRecord>> GetGrowthHabitRecordsByUserIdAndDateRangeAsync(Guid userId, DateTime startDate, DateTime endDate)
        {
            return await _unitOfWork.GrowthHabitRecordRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);
        }

        public async Task<GrowthHabitRecord> CreateGrowthHabitRecordAsync(Guid habitId, DateTime date, int value, string? notes = null)
        {
            var growthHabitRecord = new GrowthHabitRecord
            {
                Id = Guid.NewGuid(),
                HabitId = habitId,
                Date = date,
                Value = value,
                Notes = notes,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthHabitRecordRepository.AddAsync(growthHabitRecord);
            await _unitOfWork.SaveChangesAsync();

            return growthHabitRecord;
        }

        public async Task<GrowthHabitRecord?> UpdateGrowthHabitRecordAsync(Guid id, int? value, string? notes)
        {
            var growthHabitRecord = await _unitOfWork.GrowthHabitRecordRepository.GetByIdAsync(id);
            if (growthHabitRecord == null)
                return null;

            if (value.HasValue) growthHabitRecord.Value = value.Value;
            if (notes != null) growthHabitRecord.Notes = notes;

            growthHabitRecord.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthHabitRecordRepository.UpdateAsync(growthHabitRecord);
            await _unitOfWork.SaveChangesAsync();

            return growthHabitRecord;
        }

        public async Task<bool> DeleteGrowthHabitRecordAsync(Guid id)
        {
            var growthHabitRecord = await _unitOfWork.GrowthHabitRecordRepository.GetByIdAsync(id);
            if (growthHabitRecord == null)
                return false;

            await _unitOfWork.GrowthHabitRecordRepository.DeleteAsync(growthHabitRecord);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }

        public async Task<Dictionary<string, object>> GetHabitStatisticsAsync(Guid habitId, int days = 30)
        {
            var endDate = DateTime.UtcNow;
            var startDate = endDate.AddDays(-days);

            var records = await _unitOfWork.GrowthHabitRecordRepository.GetByHabitIdAndDateRangeAsync(habitId, startDate, endDate);

            var statistics = new Dictionary<string, object>
            {
                ["TotalRecords"] = records.Count(),
                ["AverageValue"] = records.Any() ? records.Average(r => r.Value) : 0,
                ["MaxValue"] = records.Any() ? records.Max(r => r.Value) : 0,
                ["MinValue"] = records.Any() ? records.Min(r => r.Value) : 0,
                ["RecordsByDate"] = records.GroupBy(r => r.Date.Date)
                                          .ToDictionary(g => g.Key.ToString("yyyy-MM-dd"), g => g.Sum(r => r.Value))
            };

            return statistics;
        }
    }

    /// <summary>
    /// 成长习惯模板服务实现
    /// </summary>
    public class GrowthHabitTemplateService : IGrowthHabitTemplateService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthHabitTemplateService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthHabitTemplate?> GetGrowthHabitTemplateByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthHabitTemplateRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthHabitTemplate>> GetAllGrowthHabitTemplatesAsync()
        {
            return await _unitOfWork.GrowthHabitTemplateRepository.GetAllAsync();
        }

        public async Task<IEnumerable<GrowthHabitTemplate>> GetGrowthHabitTemplatesByCategoryAsync(string category)
        {
            return await _unitOfWork.GrowthHabitTemplateRepository.GetByCategoryAsync(category);
        }

        public async Task<IEnumerable<GrowthHabitTemplate>> SearchGrowthHabitTemplatesAsync(string keyword)
        {
            return await _unitOfWork.GrowthHabitTemplateRepository.SearchAsync(keyword);
        }

        public async Task<GrowthHabitTemplate> CreateGrowthHabitTemplateAsync(string title, string description, string category, string frequency, int targetValue)
        {
            var growthHabitTemplate = new GrowthHabitTemplate
            {
                Id = Guid.NewGuid(),
                Title = title,
                Description = description,
                Category = category,
                Frequency = frequency,
                TargetValue = targetValue,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthHabitTemplateRepository.AddAsync(growthHabitTemplate);
            await _unitOfWork.SaveChangesAsync();

            return growthHabitTemplate;
        }

        public async Task<GrowthHabitTemplate?> UpdateGrowthHabitTemplateAsync(Guid id, string? title, string? description, string? category, string? frequency, int? targetValue)
        {
            var growthHabitTemplate = await _unitOfWork.GrowthHabitTemplateRepository.GetByIdAsync(id);
            if (growthHabitTemplate == null)
                return null;

            if (title != null) growthHabitTemplate.Title = title;
            if (description != null) growthHabitTemplate.Description = description;
            if (category != null) growthHabitTemplate.Category = category;
            if (frequency != null) growthHabitTemplate.Frequency = frequency;
            if (targetValue.HasValue) growthHabitTemplate.TargetValue = targetValue.Value;

            growthHabitTemplate.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthHabitTemplateRepository.UpdateAsync(growthHabitTemplate);
            await _unitOfWork.SaveChangesAsync();

            return growthHabitTemplate;
        }

        public async Task<bool> DeleteGrowthHabitTemplateAsync(Guid id)
        {
            var growthHabitTemplate = await _unitOfWork.GrowthHabitTemplateRepository.GetByIdAsync(id);
            if (growthHabitTemplate == null)
                return false;

            await _unitOfWork.GrowthHabitTemplateRepository.DeleteAsync(growthHabitTemplate);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }
    }

    /// <summary>
    /// 用户成长习惯模板服务实现
    /// </summary>
    public class UserGrowthHabitTemplateService : IUserGrowthHabitTemplateService
    {
        private readonly IUnitOfWork _unitOfWork;

        public UserGrowthHabitTemplateService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<UserGrowthHabitTemplate?> GetUserGrowthHabitTemplateByIdAsync(Guid id)
        {
            return await _unitOfWork.UserGrowthHabitTemplateRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<UserGrowthHabitTemplate>> GetUserGrowthHabitTemplatesByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.UserGrowthHabitTemplateRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<UserGrowthHabitTemplate>> GetUserGrowthHabitTemplatesByUserIdAndTemplateIdAsync(Guid userId, Guid templateId)
        {
            return await _unitOfWork.UserGrowthHabitTemplateRepository.GetByUserIdAndTemplateIdAsync(userId, templateId);
        }

        public async Task<UserGrowthHabitTemplate> CreateUserGrowthHabitTemplateAsync(Guid userId, Guid templateId, string? notes = null)
        {
            var userGrowthHabitTemplate = new UserGrowthHabitTemplate
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                TemplateId = templateId,
                Notes = notes,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.UserGrowthHabitTemplateRepository.AddAsync(userGrowthHabitTemplate);
            await _unitOfWork.SaveChangesAsync();

            return userGrowthHabitTemplate;
        }

        public async Task<UserGrowthHabitTemplate?> UpdateUserGrowthHabitTemplateAsync(Guid id, string? notes)
        {
            var userGrowthHabitTemplate = await _unitOfWork.UserGrowthHabitTemplateRepository.GetByIdAsync(id);
            if (userGrowthHabitTemplate == null)
                return null;

            if (notes != null) userGrowthHabitTemplate.Notes = notes;

            userGrowthHabitTemplate.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.UserGrowthHabitTemplateRepository.UpdateAsync(userGrowthHabitTemplate);
            await _unitOfWork.SaveChangesAsync();

            return userGrowthHabitTemplate;
        }

        public async Task<bool> DeleteUserGrowthHabitTemplateAsync(Guid id)
        {
            var userGrowthHabitTemplate = await _unitOfWork.UserGrowthHabitTemplateRepository.GetByIdAsync(id);
            if (userGrowthHabitTemplate == null)
                return false;

            await _unitOfWork.UserGrowthHabitTemplateRepository.DeleteAsync(userGrowthHabitTemplate);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }
    }

    /// <summary>
    /// 成长伙伴服务实现
    /// </summary>
    public class GrowthPartnerService : IGrowthPartnerService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthPartnerService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthPartner?> GetGrowthPartnerByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthPartnerRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthPartner>> GetGrowthPartnersByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthPartnerRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthPartner>> GetGrowthPartnersByUserIdAndStatusAsync(Guid userId, string status)
        {
            return await _unitOfWork.GrowthPartnerRepository.GetByUserIdAndStatusAsync(userId, status);
        }

        public async Task<GrowthPartner> CreateGrowthPartnerAsync(Guid userId, Guid partnerUserId, string? notes = null)
        {
            var growthPartner = new GrowthPartner
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                PartnerUserId = partnerUserId,
                Status = "待接受",
                Notes = notes,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthPartnerRepository.AddAsync(growthPartner);
            await _unitOfWork.SaveChangesAsync();

            return growthPartner;
        }

        public async Task<GrowthPartner?> UpdateGrowthPartnerAsync(Guid id, string? notes, string? status)
        {
            var growthPartner = await _unitOfWork.GrowthPartnerRepository.GetByIdAsync(id);
            if (growthPartner == null)
                return null;

            if (notes != null) growthPartner.Notes = notes;
            if (status != null) growthPartner.Status = status;

            growthPartner.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthPartnerRepository.UpdateAsync(growthPartner);
            await _unitOfWork.SaveChangesAsync();

            return growthPartner;
        }

        public async Task<bool> DeleteGrowthPartnerAsync(Guid id)
        {
            var growthPartner = await _unitOfWork.GrowthPartnerRepository.GetByIdAsync(id);
            if (growthPartner == null)
                return false;

            await _unitOfWork.GrowthPartnerRepository.DeleteAsync(growthPartner);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }

        public async Task<GrowthPartner?> AcceptGrowthPartnerAsync(Guid id)
        {
            var growthPartner = await _unitOfWork.GrowthPartnerRepository.GetByIdAsync(id);
            if (growthPartner == null)
                return null;

            growthPartner.Status = "已接受";
            growthPartner.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthPartnerRepository.UpdateAsync(growthPartner);
            await _unitOfWork.SaveChangesAsync();

            return growthPartner;
        }

        public async Task<GrowthPartner?> RejectGrowthPartnerAsync(Guid id)
        {
            var growthPartner = await _unitOfWork.GrowthPartnerRepository.GetByIdAsync(id);
            if (growthPartner == null)
                return null;

            growthPartner.Status = "已拒绝";
            growthPartner.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthPartnerRepository.UpdateAsync(growthPartner);
            await _unitOfWork.SaveChangesAsync();

            return growthPartner;
        }
    }

    /// <summary>
    /// 成长伙伴互动服务实现
    /// </summary>
    public class GrowthPartnerInteractionService : IGrowthPartnerInteractionService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthPartnerInteractionService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthPartnerInteraction?> GetGrowthPartnerInteractionByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthPartnerInteractionRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthPartnerInteraction>> GetGrowthPartnerInteractionsByPartnerIdAsync(Guid partnerId)
        {
            return await _unitOfWork.GrowthPartnerInteractionRepository.GetByPartnerIdAsync(partnerId);
        }

        public async Task<IEnumerable<GrowthPartnerInteraction>> GetGrowthPartnerInteractionsByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthPartnerInteractionRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthPartnerInteraction>> GetGrowthPartnerInteractionsByUserIdAndDateRangeAsync(Guid userId, DateTime startDate, DateTime endDate)
        {
            return await _unitOfWork.GrowthPartnerInteractionRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);
        }

        public async Task<GrowthPartnerInteraction> CreateGrowthPartnerInteractionAsync(Guid partnerId, Guid userId, string type, string content)
        {
            var growthPartnerInteraction = new GrowthPartnerInteraction
            {
                Id = Guid.NewGuid(),
                PartnerId = partnerId,
                UserId = userId,
                Type = type,
                Content = content,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthPartnerInteractionRepository.AddAsync(growthPartnerInteraction);
            await _unitOfWork.SaveChangesAsync();

            return growthPartnerInteraction;
        }

        public async Task<GrowthPartnerInteraction?> UpdateGrowthPartnerInteractionAsync(Guid id, string? content)
        {
            var growthPartnerInteraction = await _unitOfWork.GrowthPartnerInteractionRepository.GetByIdAsync(id);
            if (growthPartnerInteraction == null)
                return null;

            if (content != null) growthPartnerInteraction.Content = content;

            growthPartnerInteraction.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthPartnerInteractionRepository.UpdateAsync(growthPartnerInteraction);
            await _unitOfWork.SaveChangesAsync();

            return growthPartnerInteraction;
        }

        public async Task<bool> DeleteGrowthPartnerInteractionAsync(Guid id)
        {
            var growthPartnerInteraction = await _unitOfWork.GrowthPartnerInteractionRepository.GetByIdAsync(id);
            if (growthPartnerInteraction == null)
                return false;

            await _unitOfWork.GrowthPartnerInteractionRepository.DeleteAsync(growthPartnerInteraction);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }
    }

    /// <summary>
    /// 成长激励服务实现
    /// </summary>
    public class GrowthIncentiveService : IGrowthIncentiveService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthIncentiveService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthIncentive?> GetGrowthIncentiveByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthIncentiveRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthIncentive>> GetGrowthIncentivesByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthIncentiveRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthIncentive>> GetGrowthIncentivesByUserIdAndTypeAsync(Guid userId, string type)
        {
            return await _unitOfWork.GrowthIncentiveRepository.GetByUserIdAndTypeAsync(userId, type);
        }

        public async Task<IEnumerable<GrowthIncentive>> GetGrowthIncentivesByUserIdAndStatusAsync(Guid userId, string status)
        {
            return await _unitOfWork.GrowthIncentiveRepository.GetByUserIdAndStatusAsync(userId, status);
        }

        public async Task<GrowthIncentive> CreateGrowthIncentiveAsync(Guid userId, string title, string description, string type, string condition, string reward)
        {
            var growthIncentive = new GrowthIncentive
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                Title = title,
                Description = description,
                Type = type,
                Condition = condition,
                Reward = reward,
                Status = "未领取",
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthIncentiveRepository.AddAsync(growthIncentive);
            await _unitOfWork.SaveChangesAsync();

            return growthIncentive;
        }

        public async Task<GrowthIncentive?> UpdateGrowthIncentiveAsync(Guid id, string? title, string? description, string? type, string? condition, string? reward, string? status)
        {
            var growthIncentive = await _unitOfWork.GrowthIncentiveRepository.GetByIdAsync(id);
            if (growthIncentive == null)
                return null;

            if (title != null) growthIncentive.Title = title;
            if (description != null) growthIncentive.Description = description;
            if (type != null) growthIncentive.Type = type;
            if (condition != null) growthIncentive.Condition = condition;
            if (reward != null) growthIncentive.Reward = reward;
            if (status != null) growthIncentive.Status = status;

            growthIncentive.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthIncentiveRepository.UpdateAsync(growthIncentive);
            await _unitOfWork.SaveChangesAsync();

            return growthIncentive;
        }

        public async Task<bool> DeleteGrowthIncentiveAsync(Guid id)
        {
            var growthIncentive = await _unitOfWork.GrowthIncentiveRepository.GetByIdAsync(id);
            if (growthIncentive == null)
                return false;

            await _unitOfWork.GrowthIncentiveRepository.DeleteAsync(growthIncentive);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }

        public async Task<GrowthIncentive?> ClaimGrowthIncentiveAsync(Guid id)
        {
            var growthIncentive = await _unitOfWork.GrowthIncentiveRepository.GetByIdAsync(id);
            if (growthIncentive == null)
                return null;

            growthIncentive.Status = "已领取";
            growthIncentive.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthIncentiveRepository.UpdateAsync(growthIncentive);
            await _unitOfWork.SaveChangesAsync();

            return growthIncentive;
        }
    }

    /// <summary>
    /// 成长激励记录服务实现
    /// </summary>
    public class GrowthIncentiveRecordService : IGrowthIncentiveRecordService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthIncentiveRecordService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<GrowthIncentiveRecord?> GetGrowthIncentiveRecordByIdAsync(Guid id)
        {
            return await _unitOfWork.GrowthIncentiveRecordRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<GrowthIncentiveRecord>> GetGrowthIncentiveRecordsByIncentiveIdAsync(Guid incentiveId)
        {
            return await _unitOfWork.GrowthIncentiveRecordRepository.GetByIncentiveIdAsync(incentiveId);
        }

        public async Task<IEnumerable<GrowthIncentiveRecord>> GetGrowthIncentiveRecordsByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.GrowthIncentiveRecordRepository.GetByUserIdAsync(userId);
        }

        public async Task<IEnumerable<GrowthIncentiveRecord>> GetGrowthIncentiveRecordsByUserIdAndDateRangeAsync(Guid userId, DateTime startDate, DateTime endDate)
        {
            return await _unitOfWork.GrowthIncentiveRecordRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);
        }

        public async Task<GrowthIncentiveRecord> CreateGrowthIncentiveRecordAsync(Guid incentiveId, Guid userId, string type, string description)
        {
            var growthIncentiveRecord = new GrowthIncentiveRecord
            {
                Id = Guid.NewGuid(),
                IncentiveId = incentiveId,
                UserId = userId,
                Type = type,
                Description = description,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };

            await _unitOfWork.GrowthIncentiveRecordRepository.AddAsync(growthIncentiveRecord);
            await _unitOfWork.SaveChangesAsync();

            return growthIncentiveRecord;
        }

        public async Task<GrowthIncentiveRecord?> UpdateGrowthIncentiveRecordAsync(Guid id, string? description)
        {
            var growthIncentiveRecord = await _unitOfWork.GrowthIncentiveRecordRepository.GetByIdAsync(id);
            if (growthIncentiveRecord == null)
                return null;

            if (description != null) growthIncentiveRecord.Description = description;

            growthIncentiveRecord.UpdatedAt = DateTime.UtcNow;

            await _unitOfWork.GrowthIncentiveRecordRepository.UpdateAsync(growthIncentiveRecord);
            await _unitOfWork.SaveChangesAsync();

            return growthIncentiveRecord;
        }

        public async Task<bool> DeleteGrowthIncentiveRecordAsync(Guid id)
        {
            var growthIncentiveRecord = await _unitOfWork.GrowthIncentiveRecordRepository.GetByIdAsync(id);
            if (growthIncentiveRecord == null)
                return false;

            await _unitOfWork.GrowthIncentiveRecordRepository.DeleteAsync(growthIncentiveRecord);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }
    }

    /// <summary>
    /// 成长统计服务实现
    /// </summary>
    public class GrowthStatisticsService : IGrowthStatisticsService
    {
        private readonly IUnitOfWork _unitOfWork;

        public GrowthStatisticsService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<Dictionary<string, object>> GetGrowthStatisticsAsync(Guid userId, int days = 30)
        {
            var endDate = DateTime.UtcNow;
            var startDate = endDate.AddDays(-days);

            var goals = await _unitOfWork.GrowthGoalRepository.GetByUserIdAsync(userId);
            var habits = await _unitOfWork.GrowthHabitRepository.GetByUserIdAsync(userId);
            var milestones = await _unitOfWork.GrowthMilestoneRepository.GetByUserIdAsync(userId);
            var records = await _unitOfWork.GrowthRecordRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);

            var statistics = new Dictionary<string, object>
            {
                ["TotalGoals"] = goals.Count(),
                ["CompletedGoals"] = goals.Count(g => g.Status == "已完成"),
                ["ActiveGoals"] = goals.Count(g => g.Status == "进行中"),
                ["TotalHabits"] = habits.Count(),
                ["ActiveHabits"] = habits.Count(h => h.IsActive),
                ["TotalMilestones"] = milestones.Count(),
                ["RecentRecords"] = records.Count(),
                ["GoalsByCategory"] = goals.GroupBy(g => g.Category)
                                          .ToDictionary(g => g.Key, g => g.Count()),
                ["HabitsByCategory"] = habits.GroupBy(h => h.Category)
                                           .ToDictionary(h => h.Key, h => h.Count())
            };

            return statistics;
        }

        public async Task<Dictionary<string, object>> GetGoalCompletionStatisticsAsync(Guid userId, int days = 30)
        {
            var endDate = DateTime.UtcNow;
            var startDate = endDate.AddDays(-days);

            var goals = await _unitOfWork.GrowthGoalRepository.GetByUserIdAsync(userId);
            var completedGoals = goals.Where(g => g.Status == "已完成" && g.UpdatedAt >= startDate).ToList();

            var statistics = new Dictionary<string, object>
            {
                ["TotalGoals"] = goals.Count(),
                ["CompletedGoals"] = completedGoals.Count(),
                ["CompletionRate"] = goals.Count() > 0 ? (double)completedGoals.Count() / goals.Count() * 100 : 0,
                ["AvgCompletionTime"] = completedGoals.Any() ? 
                    completedGoals.Average(g => (g.UpdatedAt - g.CreatedAt).TotalDays) : 0,
                ["GoalsByPriority"] = goals.GroupBy(g => g.Priority)
                                          .ToDictionary(g => g.Key, g => new
                                          {
                                              Total = g.Count(),
                                              Completed = g.Count(x => x.Status == "已完成")
                                          })
            };

            return statistics;
        }

        public async Task<Dictionary<string, object>> GetHabitPersistenceStatisticsAsync(Guid userId, int days = 30)
        {
            var endDate = DateTime.UtcNow;
            var startDate = endDate.AddDays(-days);

            var habits = await _unitOfWork.GrowthHabitRepository.GetByUserIdAsync(userId);
            var habitRecords = new List<GrowthHabitRecord>();

            foreach (var habit in habits)
            {
                var records = await _unitOfWork.GrowthHabitRecordRepository.GetByHabitIdAndDateRangeAsync(habit.Id, startDate, endDate);
                habitRecords.AddRange(records);
            }

            var statistics = new Dictionary<string, object>
            {
                ["TotalHabits"] = habits.Count(),
                ["ActiveHabits"] = habits.Count(h => h.IsActive),
                ["TotalRecords"] = habitRecords.Count(),
                ["AvgRecordsPerHabit"] = habits.Count() > 0 ? (double)habitRecords.Count() / habits.Count() : 0,
                ["HabitsByFrequency"] = habits.GroupBy(h => h.Frequency)
                                             .ToDictionary(h => h.Key, h => h.Count()),
                ["RecordsByDate"] = habitRecords.GroupBy(r => r.Date.Date)
                                               .ToDictionary(g => g.Key.ToString("yyyy-MM-dd"), g => g.Count())
            };

            return statistics;
        }

        public async Task<Dictionary<string, object>> GetGrowthTrendStatisticsAsync(Guid userId, int days = 30)
        {
            var endDate = DateTime.UtcNow;
            var startDate = endDate.AddDays(-days);

            var goals = await _unitOfWork.GrowthGoalRepository.GetByUserIdAsync(userId);
            var habits = await _unitOfWork.GrowthHabitRepository.GetByUserIdAsync(userId);
            var milestones = await _unitOfWork.GrowthMilestoneRepository.GetByUserIdAsync(userId);
            var records = await _unitOfWork.GrowthRecordRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);

            var statistics = new Dictionary<string, object>
            {
                ["GoalsCreated"] = goals.Count(g => g.CreatedAt >= startDate),
                ["GoalsCompleted"] = goals.Count(g => g.Status == "已完成" && g.UpdatedAt >= startDate),
                ["HabitsCreated"] = habits.Count(h => h.CreatedAt >= startDate),
                ["MilestonesAchieved"] = milestones.Count(m => m.AchievedDate >= startDate),
                ["RecordsCreated"] = records.Count(),
                ["DailyActivity"] = Enumerable.Range(0, days)
                    .Select(i => endDate.AddDays(-i).Date)
                    .Reverse()
                    .ToDictionary(d => d.ToString("yyyy-MM-dd"), d => records.Count(r => r.CreatedAt.Date == d))
            };

            return statistics;
        }
    }
}